﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Linq.Expressions;
using SalesSys.Comm.DAL;
using SalesSys.DAL.Base;

namespace SalesSys.DAL
{
    

    [PartCreationPolicy(CreationPolicy.NonShared)]
    [Export(typeof(IRepository<>))]
    public class WXEFRepository<TEntity> : EFRepositoryBase<TEntity, EFContextWarp<WXDbContext>>
        where TEntity : class, IDbEntity
    {
        

    }

    [PartCreationPolicy(CreationPolicy.NonShared)]
    [Export(typeof(ITreeRepository<>))]
    public class TreeRepository<TEntity> : WXEFRepository<TEntity>, ITreeRepository<TEntity> where TEntity : class, IDbEntity, ITreeEntity<Guid, TEntity>
    {

        /// <summary>
        ///     删除指定编号的记录
        /// </summary>
        /// <param name="id"> 实体记录编号 </param>
        /// <param name="isSave"> 是否执行保存 </param>
        /// <returns> 操作影响的行数 </returns>
        public override int Delete(object id, bool isSave = true)
        {

            TEntity entity = EFContext.Set<TEntity>().Find(id);
            return entity != null ? Delete(entity, isSave) : 0;
        }

        public static ICollection<ITreeEntity> GetAllTreeEntities(ITreeEntity entity)
        {
            var list = new List<ITreeEntity>();
            if (entity == null) return list;

            list.Add(entity);
            if (entity.Children.Count() > 0)
            {
                foreach (var c in entity.Children)
                {
                    list.AddRange(GetAllTreeEntities(c));
                }
            }
            return list;
        }

        /// <summary>
        ///     删除实体记录
        /// </summary>
        /// <param name="entity"> 实体对象 </param>
        /// <param name="isSave"> 是否执行保存 </param>
        /// <returns> 操作影响的行数 </returns>
        public override int Delete(TEntity entity, bool isSave = true)
        {
            var list = GetAllTreeEntities(entity).Distinct().ToArray();

            for (int i = list.Length -1; i >= 0; i--)
            {
                var a = list[i];
                EFContext.RegisterDeleted(a);
            }
           
            return isSave ? EFContext.Commit() : 0;

            //EFContext.RegisterDeleted<TEntity>(entity);
            //return isSave ? EFContext.Commit() : 0;
        }

        /// <summary>
        ///     删除实体记录集合
        /// </summary>
        /// <param name="entities"> 实体记录集合 </param>
        /// <param name="isSave"> 是否执行保存 </param>
        /// <returns> 操作影响的行数 </returns>
        public override int Delete(IEnumerable<TEntity> entities, bool isSave = true)
        {
            var list = new List<ITreeEntity>();
            IEnumerable<TEntity> enumerable = entities as TEntity[] ?? entities.ToArray();
            foreach (var ent in enumerable)
            {
                list.AddRange(GetAllTreeEntities(ent));
            }

            EFContext.RegisterDeleted(list.Distinct());
            return isSave ? EFContext.Commit() : 0;
        }

        /// <summary>
        ///     删除所有符合特定表达式的数据
        /// </summary>
        /// <param name="predicate"> 查询条件谓语表达式 </param>
        /// <param name="isSave"> 是否执行保存 </param>
        /// <returns> 操作影响的行数 </returns>
        public override int Delete(Expression<Func<TEntity, bool>> predicate, bool isSave = true)
        {
            var list = new List<ITreeEntity>();

            List<TEntity> entities = EFContext.Set<TEntity>().Where(predicate).ToList();
            foreach (var ent in entities)
            {
                list.AddRange(GetAllTreeEntities(ent));
            }
            return entities.Count > 0 ? Delete(entities, isSave) : 0;
        }

    }


   
}
